package com.ruoyi.mp.service.impl;

import com.alibaba.nacos.common.utils.CollectionUtils;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.common.core.utils.bean.BeanUtils;
import com.ruoyi.mp.domain.MpMenu;
import com.ruoyi.mp.domain.MpMenuVo;
import com.ruoyi.mp.mapper.MpMenuMapper;
import com.ruoyi.mp.service.IMpAccountService;
import com.ruoyi.mp.service.IMpMenuService;
import me.chanjar.weixin.common.bean.menu.WxMenu;
import me.chanjar.weixin.common.bean.menu.WxMenuButton;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.mp.api.WxMpService;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 公众号菜单Service业务层处理
 *
 * @author ruoyi
 * @date 2023-11-18
 */
@Service
public class MpMenuServiceImpl implements IMpMenuService
{
    @Autowired
    private MpMenuMapper mpMenuMapper;
    @Autowired
    private IMpAccountService iMpAccountService;
    @Autowired
    private WxMpService wxService;
    /**
     * 查询公众号菜单
     *
     * @param id 公众号菜单主键
     * @return 公众号菜单
     */
    @Override
    public MpMenu selectMpMenuById(Long id)
    {
        return mpMenuMapper.selectMpMenuById(id);
    }

    /**
     * 查询公众号菜单列表
     *
     * @param mpMenu 公众号菜单
     * @return 公众号菜单
     */
    @Override
    public List<MpMenu> selectMpMenuList(MpMenu mpMenu)
    {
        return mpMenuMapper.selectMpMenuList(mpMenu);
    }



    /**
     * 批量删除公众号菜单
     *
     * @param ids 需要删除的公众号菜单主键
     * @return 结果
     */
    @Override
    public int deleteMpMenuByIds(Long[] ids)
    {
        return mpMenuMapper.deleteMpMenuByIds(ids);
    }



    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveMenu(MpMenuVo createReqVO) throws WxErrorException {
        String appId = this.iMpAccountService.selectMpAccountById(createReqVO.getAccountId()).getAppId();
        WxMenu wxMenu =  this.valiteMenu(createReqVO);
        this.wxService.switchover(appId);
        this.wxService.getMenuService().menuCreate(wxMenu);
        this.mpMenuMapper.deleteMpMenuByAppId(appId);
        List<MpMenuVo.menu> menu = createReqVO.getMenus();
        menu.forEach(menuItem ->{
            menuItem.setAccountId(createReqVO.getAccountId());
            menuItem.setAppId(appId);
            this.mpMenuMapper.insertMpMenu(menuItem);
            Long partId = menuItem.getId();
            if(CollectionUtils.isEmpty(menuItem.getChildren())){
                return;
            }
            menuItem.getChildren().forEach(child ->{
                child.setAccountId(createReqVO.getAccountId());
                child.setAppId(appId);
                child.setParentId(String.valueOf(partId));
                this.mpMenuMapper.insertMpMenu(child);
            });
        });
    }


    public  WxMenu  valiteMenu(MpMenuVo createReqVO){
        WxMenu menus = new WxMenu();
        List<MpMenuVo.menu> menu = createReqVO.getMenus();
        for (int i = 0; i < menu.size() ;i++){
            WxMenuButton wxMenuButton = new WxMenuButton();
            BeanUtils.copyProperties(menu.get(i),wxMenuButton);
            if (StringUtils.isNotEmpty(menu.get(i).getMenuKey())) {
                wxMenuButton.setKey(menu.get(i).getMenuKey());
            }
            if (StringUtils.isNotEmpty(menu.get(i).getMiniProgramAppId())) {
                wxMenuButton.setAppId(menu.get(i).getMiniProgramAppId());
                wxMenuButton.setPagePath(menu.get(i).getMiniProgramPagePath());
            }
            if (StringUtils.isNotEmpty(menu.get(i).getReplyMediaId())) {
                wxMenuButton.setMediaId(menu.get(i).getReplyMediaId());
            }
            if(StringUtils.isNotEmpty(menu.get(i).getUrl())){
                wxMenuButton.setUrl(menu.get(i).getUrl());
            }
            menus.getButtons().add(wxMenuButton);
            if(ObjectUtils.isNotEmpty(menu.get(i).getChildren())){
                for(int j=0;j<menu.get(i).getChildren().size();j++){
                    WxMenuButton wxMenuButton2 = new WxMenuButton();
                    BeanUtils.copyProperties(menu.get(i).getChildren().get(j),wxMenuButton2);
                    if (StringUtils.isNotEmpty(menu.get(i).getChildren().get(j).getMenuKey())) {
                        wxMenuButton2.setKey(menu.get(i).getChildren().get(j).getMenuKey());
                    }
                    if (StringUtils.isNotEmpty(menu.get(i).getChildren().get(j).getMiniProgramAppId())) {
                        wxMenuButton2.setAppId(menu.get(i).getChildren().get(j).getMiniProgramAppId());
                        wxMenuButton2.setPagePath(menu.get(i).getChildren().get(j).getMiniProgramPagePath());
                    }
                    if (StringUtils.isNotEmpty(menu.get(i).getChildren().get(j).getReplyMediaId())) {
                        wxMenuButton2.setMediaId(menu.get(i).getChildren().get(j).getReplyMediaId());
                    }
                    if(StringUtils.isNotEmpty(menu.get(i).getChildren().get(j).getUrl())){
                        wxMenuButton2.setUrl(menu.get(i).getChildren().get(j).getUrl());
                    }
                    wxMenuButton.getSubButtons().add(wxMenuButton2);
                }
            }
        }
        return  menus;
    }
}
